package com.zyz.service.impl;

import java.util.List;

import com.zyz.dao.IUserDAO;
import com.zyz.model.User;
import com.zyz.service.IUserService;

public class UserSerivce implements IUserService {
	private IUserDAO userDAO;
	
	public IUserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	/* (non-Javadoc)
	 * @see com.zyz.service.impl.IUserService#saveUser(com.zyz.model.User)
	 */
	/* (non-Javadoc)
	 * @see com.zyz.service.impl.IUserService#saveUser(com.zyz.model.User)
	 */
	@Override
	public void saveUser(User user){
		System.out.println("save!!");
		userDAO.save(user);
	}
	
	/* (non-Javadoc)
	 * @see com.zyz.service.impl.IUserService#findById(int)
	 */
	@Override
	public User findById(int id){
		return userDAO.findById(id);
	}

	@Override
	public User checkUser(String userName, String password) throws Exception {
		// TODO Auto-generated method stub
		List<User> users = userDAO.findByName(userName);
		if(users== null || users.size() == 0){
			throw new Exception("no user");
		}
		for (int i = 0; i < users.size(); i++) {
			User user = users.get(i);
			if(user!= null && user.getPassword().equals(password)){
				return user;
			}
		}
		throw new Exception("password error");
	}
	

}
